package lab_2;

import java.net.Socket;


/**
 * Class is Identificator Of Server
 * @author pacific
 */
public class ServerID {
   
    
    public ServerID( String host, int port, String alias ) {
        setHost( host ) ;
        setPort( port ) ;
        setAlias( alias ) ;
    }


    /** Try connect with this server */
    public Socket connect() {
        Socket socket = null ;
        try {
            socket = new Socket( getHost(), getPort() ) ;
        } catch ( Exception ex ) {
            MyLogger.putMessange( "Not can get connect with Scoekt. " + ex ) ;
        }
        if( socket != null )
            setConnected( true );
        return socket ;
    }


    @Override
    public String toString() {
        return getAlias() + "/" + getHost() + ":" + getPort() ;
    }


    @Override
    public boolean equals(Object obj) {
        if( obj == this )
            return true ;
        if( obj instanceof ServerID ) {
            ServerID id = (ServerID) obj ;
            if(     getHost() == id.getHost() &&
                    getPort() == id.getPort() )
                return true ;
        }
        return false ;
    }


    @Override
    public int hashCode() {
        return getHost().hashCode() + getPort() ;
    }


    //-----Fields-----

    private boolean isConnected = false ;
    private int port;
    private String host;
    private String alias ;


    //-----Getters and Setters-----

    //For "port"
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    //For "host"
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    //For "alias"
    public String getAlias() {
        return alias;
    }
    public void setAlias(String alias) {
        this.alias = alias;
    }
    //For "isConnected"
    public boolean isConnected() {
        return isConnected;
    }
    public void setConnected( boolean isConnected ) {
        this.isConnected = isConnected;
    }
}